{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85ab7d21-3521-4cd9-b8b1-4ee2dc3e8391",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae607461-7578-4ec3-b8f1-61e874140f13",
   "metadata": {},
   "source": [
    "### 构造出来一个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "30a9d2e7-24fd-4b0f-9dd2-752f4940efda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e3e0413b-aa8d-4093-9e22-8779af9d3b85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10, 12, 14, 16, 18])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(2,20,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3b37e7c1-44e8-4cd0-bfc2-5f33215c1c9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18.], dtype=float32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(2,20,2,dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a2c6e188-9abe-405a-ab10-ab49ec9acdce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,\n",
       "        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "519ef630-6628-4872-bb57-d6389e6dc17c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  1.77827941,  3.16227766,  5.62341325, 10.        ])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,1,5)  ### 默认以10为底，生成对数等比数列\n",
    "                    ### 生成从 10的0次方 ~ 10的1次方 的 5 个元素的等比数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "127fa917-b43b-4da4-82e0-edc3ad411fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-10.,  -5.,   0.,   5.,  10.])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.linspace(-10,10,5)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "44993381-aa5f-4153-8370-136d713a2753",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-10.,  -5.,   0.,   5.,  10.])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.linspace(-10,10,5)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "13b086ac-3f20-4041-981c-09bc8613f7e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = np.meshgrid(x,y)  ### 将数组按行扩展为矩阵，x和 y都扩展到相同的行和列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b3ac6527-131f-4686-b071-23db40155391",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10.,  -5.,   0.,   5.,  10.],\n",
       "       [-10.,  -5.,   0.,   5.,  10.],\n",
       "       [-10.,  -5.,   0.,   5.,  10.],\n",
       "       [-10.,  -5.,   0.,   5.,  10.],\n",
       "       [-10.,  -5.,   0.,   5.,  10.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "91b94e2e-57b2-4ee2-83a5-9eb4da07570a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10., -10., -10., -10., -10.],\n",
       "       [ -5.,  -5.,  -5.,  -5.,  -5.],\n",
       "       [  0.,   0.,   0.,   0.,   0.],\n",
       "       [  5.,   5.,   5.,   5.,   5.],\n",
       "       [ 10.,  10.,  10.,  10.,  10.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8e505cdd-d182-47cc-8d57-cbf5c9227daf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[0:10:1] ### 用于快速生成数组，功能等同于 arange，但更简洁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d96708ce-6687-45bd-bc4f-523b48fa5d63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6],\n",
       "       [7],\n",
       "       [8],\n",
       "       [9]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[0:10:1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27376da1-8ce4-45b2-a098-2dd7d2926f74",
   "metadata": {},
   "source": [
    "### 更常用的 zeros ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6f35e3e3-2672-4a66-b9be-250faac7f5c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0.])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "64c51cc6-b16e-46b4-92d7-839a79a9e7d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "eff9afea-d243-43cf-8eb1-afd3e0046850",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "918382a5-d263-4765-9de8-97fa4964ef8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 8., 8.],\n",
       "       [8., 8., 8.],\n",
       "       [8., 8., 8.]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,3)) * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b52de0eb-68dc-4097-b644-62bedd7dc326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,3),dtype = np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5d1d52f8-fd35-41be-8cbc-67d757c97a6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6,)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.empty(6)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "79f981a1-c98b-4054-b3ae-36a8902120e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.fill(1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "cba70f6d-37a8-41c9-a313-b66c1285df60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.array([1,2,3,4])\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1fd66f4e-0096-4fa5-a6e4-65f79ad4d40a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros_like(liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "9fce94cd-c0ce-4b42-b1f0-582f63b4fbc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "75bcb47c-5a3d-4dd8-a907-37fe4ffbd34d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(5)  ### 用于生成单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3eba32f-336a-4059-859b-23e3f81555c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
